package com.example.demo004springcache.service;

import com.example.demo004springcache.domain.User;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@CacheConfig(cacheNames = {"user"})
public class UserService {

    @Cacheable(key = "#root.method")
    public User getUserById(){
        User user = new User(1, "zhangsan", "123456", "张三");
        System.out.println("查询数据库...");
        return user;
    }
    @Cacheable(key = "#root.method")
    public List<User> getUserList(){
        User user1 = new User(1, "zhangsan", "123456", "张三");
        User user2 = new User(2, "lisi", "123456", "李四");
        System.out.println("查询数据库...");
        return new ArrayList<>(Arrays.asList(user1,user2));
    }

}
